﻿using System.Collections.Generic;
using System.Linq;
using DroidClaim.DAL.Entities;
using NHibernate;
using NHibernate.Criterion;
using System;
using DroidClaim.DAL.Models;


namespace DroidClaim.DAL.Dal
{
    public class SessionInfoDAL
    {
        private readonly ISession _session;
        public SessionInfoDAL(ISession session)
        {
             this._session = session;
        }

        public List<SessionInfo> GetAll(SessionInfoSearch searchCriteria)
        {
            var criteria = _session.CreateCriteria(typeof(SessionInfo));

            if (searchCriteria != null)
            {
                // Search	
		
									
											if (!string.IsNullOrEmpty(searchCriteria.SessionId))
						{
							criteria.Add(Expression.Like("SessionId", searchCriteria.SessionId, MatchMode.Start));
						}
						
															
											if (!string.IsNullOrEmpty(searchCriteria.RegistrationId))
						{
							criteria.Add(Expression.Like("RegistrationId", searchCriteria.RegistrationId, MatchMode.Start));
						}
						
										
			

                //Sort
                if (!string.IsNullOrEmpty(searchCriteria.Sort))
                {
                    if (!string.IsNullOrEmpty(searchCriteria.SortType) && searchCriteria.SortType.ToLower() == "desc")
                    {
                        criteria.AddOrder(Order.Desc(searchCriteria.Sort));
                    }
                    else
                    {
                        criteria.AddOrder(Order.Asc(searchCriteria.Sort));
                    }

                }
            }

            List<SessionInfo> result = criteria.List<SessionInfo>().ToList();
            if (result == null || result.Count <= 0)
            {
                return result;
            }

            if (searchCriteria.Customer != null)
            {
                List<SessionInfo> customerList = result.FindAll(a => a.Customer != null);
                result = customerList.FindAll(a => a.Customer.LicensePlate == searchCriteria.Customer.LicensePlate);
            }

            if (searchCriteria.Surveyor != null)
            {
                List<SessionInfo> surveyorList = result.FindAll(a => a.Surveyor != null);
                result = surveyorList.FindAll(a => a.Surveyor.Username == searchCriteria.Surveyor.Username);
            }

            return result;
        }

        public void Add(SessionInfo sessioninfo)
        {
            _session.Save( sessioninfo);
        }

        public void Update(SessionInfo sessioninfo)
        {
			 sessioninfo = _session.Merge(sessioninfo);
            _session.Update( sessioninfo);
        }

        public void Delete(List<string> idList)
        {
            //int row = _session.CreateQuery("DELETE SessionInfo where Id in (:IdList)").SetParameterList("IdList", idList).ExecuteUpdate();
            int row = _session.CreateQuery("DELETE SessionInfo where SessionId in (:IdList)").SetParameterList("IdList", idList).ExecuteUpdate();
            if (row != idList.Count)
                throw new Exception("Delete Not Success");

        }
    }
}

